/*
 * cpu.hpp
 *
 *  Created on: Jan 19, 2012
 *      Author: douglas
 */

#ifndef CPU_HPP_
#define CPU_HPP_

#include <playlib/defines.hpp>
#include <z80/z80.hpp>
#include <QTextEdit>
#include <QtCore>
#include <string>
#include <iostream>
#include <stdio.h>
#include <string.h>


#define MEMORY_SIZE 4096
#define MAX_OPCODES_PER_PROGRAM 15000

class cpu : public z80 {
private:
    //Size Memory for this CPU
    u8 Memory[MEMORY_SIZE];

	u32 programSize;
	bool runningProgram;
    bool hasProgramLoaded;

    QTextEdit *_txtEdit;

public:

	cpu(){
		programSize = 0;
		runningProgram = false;
        hasProgramLoaded = false;
	}

    cpu(QTextEdit *txtEdit){
        programSize = 0;
        runningProgram = false;
        hasProgramLoaded = false;
        _txtEdit = txtEdit;
    }

	void reset();
    void reset_program();

	virtual u8 read8(u16 address, int cycles);
	virtual void write8(u16 address, u8 data, int cycles);
	virtual void sync(int cycles);
    virtual int doDebug(const std::string& Msg);


	virtual void retCallback(int cycles);

    u32 load_and_run_program(const char *);
    u32 run_loaded_program();
	u32 load_program(const char *);
    void cpu_step();


	void test_registers();
	void test_processor();
	void test_memory();
	void memoryDump(u16 start, u16 size);
	void registersDump();
    u8 getReg(u32 reg);
    void setReg(u32 reg, u8 value);
    u16 getPC();
    void setPC(u16 value);
    u16 getSP();
    u16 setSP(u16 value);
    u16 getRegPair(u32 reg);
    void setRegPair(u32 reg, u16 value);



private slots:
    void on_txtRegF_textChanged(const QString &arg1);
    void on_txtRegE_textChanged(const QString &arg1);
    void on_actionStep_triggered();
};


#endif /* CPU_HPP_ */
